home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / kyocera / util.c < prev    next >
C/C++ Source or Header  |  1990-10-01  |  3KB  |  158 lines

  1. #include <stdio.h>
  2. #include "util.h"
  3. #include "dev.h"
  4.  
  5. char *outfname, *infname, *user, *host, *pgmnam, efn[256];
  6. FILE *ef;
  7. int olderrfd,debugging;
  8.  
  9. /* Utility procedures
  10.  */
  11.  
  12. open_ef()
  13. {
  14.   if (!debugging) {
  15.     sprintf(efn,"/usr/tmp/%s.err.XXXXXX",pgmnam);
  16.     if (!(ef = fopen(mktemp(efn),"w+"))) croak("couldn't open %s",efn);
  17.     unlink(efn);
  18.     fflush(stderr); olderrfd = dup(2); close(2); dup(fileno(ef));
  19.   }
  20. }
  21.  
  22. close_ef()
  23. {
  24.   long fpos;
  25.  
  26.   if (ef) {
  27.     /* Now put back the old stderr */
  28.     (void) fflush(stderr);
  29.     fpos = ftell(stderr);
  30.     close(2); dup(olderrfd); close(olderrfd);
  31.     /* And print out anything that was sent to stderr */
  32.     if (fpos > 0) {
  33.       rewind(ef);
  34.       dev_print_log(ef);
  35.     }
  36.     (void) fclose(ef);
  37.     ef = NULL;
  38.   }
  39. }
  40.  
  41. /*VARARGS1*/
  42. debug(fmt,a,b,c,d,e)
  43.      char *fmt;
  44.      long a,b,c,d,e;
  45. {
  46.     if (debugging) fprintf(stderr,fmt,a,b,c,d,e);
  47. }
  48.  
  49. /*VARARGS1*/
  50. croak(fmt,a,b,c,d,e)
  51.      char *fmt;
  52.      long a,b,c,d,e;
  53. {
  54.   fprintf(stderr,"%s: ",pgmnam);
  55.   fprintf(stderr,fmt,a,b,c,d,e);
  56.   fprintf(stderr,"\n");
  57.   if (debugging) {
  58.     perror("last error");
  59.     if (infname) fprintf(stderr,"input file %s\n",infname);
  60.     if (outfname) fprintf(stderr,"output file %s\n",outfname);
  61.     if (user && host) fprintf(stderr,"running for %s@%s\n",user,host);
  62.   }
  63.   close_ef();
  64.   exit(0);
  65. }
  66.  
  67. swallow(nchars,f)
  68.      long nchars;
  69.      FILE *f;
  70. {
  71.   while (nchars-- > 0) (void) getc(f);
  72. }
  73.  
  74. unsigned long get2(f)
  75.      FILE *f;
  76. {
  77.   register unsigned long x;
  78.  
  79.   x = getc(f);
  80.   x = (x << 8) | getc(f);
  81.   return(x);
  82. }
  83.  
  84. unsigned long get3(f)
  85.      FILE *f;
  86. {
  87.   register unsigned long x;
  88.  
  89.   x = getc(f);
  90.   x = (x << 8) | getc(f);
  91.   x = (x << 8) | getc(f);
  92.   return(x);
  93. }
  94.  
  95. unsigned long get4(f)
  96.      FILE *f;
  97. {
  98.   register unsigned long x;
  99.  
  100.   x = getc(f);
  101.   x = (x << 8) | getc(f);
  102.   x = (x << 8) | getc(f);
  103.   x = (x << 8) | getc(f);
  104.   return(x);
  105. }
  106.  
  107. long sget2(f)
  108.      FILE *f;
  109. {
  110.   register long x;
  111.  
  112.   x = ((long) ((char) getc(f)));
  113.   x = (x << 8) | getc(f);
  114.   return(x);
  115. }
  116.  
  117. long sget3(f)
  118.      FILE *f;
  119. {
  120.   register long x;
  121.  
  122.   x = ((long) ((char) getc(f)));
  123.   x = (x << 8) | getc(f);
  124.   x = (x << 8) | getc(f);
  125.   return(x);
  126. }
  127.  
  128. long sget4(f)
  129.      FILE *f;
  130. {
  131.   register long x;
  132.  
  133.   x = ((long) ((char) getc(f)));
  134.   x = (x << 8) | getc(f);
  135.   x = (x << 8) | getc(f);
  136.   x = (x << 8) | getc(f);
  137.   return(x);
  138. }
  139.  
  140. put4(l,f)
  141.      unsigned long l;
  142.      FILE *f;
  143. {
  144.   putc((l >> 24) & 0377,f);
  145.   putc((l >> 16) & 0377,f);
  146.   putc((l >> 8) & 0377,f);
  147.   putc(l & 0377,f);
  148. }
  149.  
  150. put2(s,f)
  151.      unsigned short s;
  152.      FILE *f;
  153. {
  154.   putc((s >> 8) & 0377,f);
  155.   putc(s & 0377,f);
  156. }
  157.  
  158.